package cn.trevet.minecraft.limitation.event;

import cn.trevet.minecraft.limitation.vo.NearingBase;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TrevetEventBase implements Listener {
    private final boolean enabled;
    private final Map<String, NearingBase> nearingEntityMap = new HashMap<>();
    private final boolean debug;

    private final JavaPlugin plugin;
    private final List<BlockFace> blockFaces = Arrays.asList(BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST);

    public TrevetEventBase(boolean enabled, boolean debug, JavaPlugin plugin) {
        this.enabled = enabled;
        this.debug = debug;
        this.plugin = plugin;
    }

    public void debug(String s) {
        if (!this.debug) {
            this.plugin.getLogger().info(String.format("[DEBUG]: %s", s));
        }
    }

    public Map<String, NearingBase> getNearingEntityMap() {
        return nearingEntityMap;
    }

    /**
     * @return 预留
     */
    public JavaPlugin getPlugin() {
        return plugin;
    }

    @EventHandler
    public void onBlockPlaceEvent(BlockPlaceEvent event) {
        // 防止意外运行
        if (!this.enabled) {
            return;
        }
        Block block = event.getBlockPlaced();
        this.debug(String.format("放置事件，放置物品为：%s", block.getType().name()));

        List<String> list = this.nearingEntityMap.keySet().stream().filter(s -> block.getType().name().startsWith(s)).collect(Collectors.toList());
        if (!list.isEmpty()) {
            this.debug("物品查询为真，开始查询获取周围6个方块");
            List<Block> blockTemp = this.blockFaces.stream().map(block::getRelative).collect(Collectors.toList());

            // 遍历查询结果,一般情况下不会存在多个重复的方块名称前缀
            for (String namekey : list) {
                NearingBase nearingBase = this.nearingEntityMap.get(namekey);

                // 先获取对立列表
                List<String> temps;
                if (nearingBase.getPrefixA().stream().anyMatch(s -> s.equals(namekey))) {
                    temps = nearingBase.getPrefixB();
                } else {
                    temps = nearingBase.getPrefixA();
                }

                // 检查 blockTemp列表中是否存在对立方块
                if (blockTemp.stream().anyMatch(block1 ->
                        temps.stream().anyMatch(s -> block1.getType().name().startsWith(s))
                )) {
                    // 取消事件
                    this.debug(String.format("玩家 %s 尝试放置方块《%s》被拒绝。", event.getPlayer().getName(), block.getType().name()));
                    event.setCancelled(true);
                    // 原先写死的消息： "你干嘛！哎哟！"
                    event.getPlayer().sendMessage(nearingBase.getRejectMessages());
                }
            }
        }
    }

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent event) {
        event.setJoinMessage(String.format("玩家 %s 加入了游戏", event.getPlayer().getName()));
    }

    @EventHandler
    public void onPlayerQuitEvent(PlayerQuitEvent event) {
        event.setQuitMessage(String.format("玩家 %s 离开了游戏", event.getPlayer().getName()));
    }

}
